package cn.edu.sdjzu.xg11.web;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import cn.edu.sdjzu.xg11.domain.GraduateProject;
import cn.edu.sdjzu.xg11.domain.Major;
import constants.CONSTANTS;

public class GraduateProjectExcelView extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setHeader("Content-Disposition", 
				"attachment;filename="+ new String(
												"题目申报列表.xls".getBytes(), 
												"iso8859-1"));
		
		@SuppressWarnings("unchecked")
		//从model中获得列表
		Set<GraduateProject> graduateProjectList = (Set<GraduateProject>) model
				.get("graduateProjectList");
		

		//创建一个Excel的sheet
		HSSFSheet sheet = workbook.createSheet("毕业设计列表");
		//设置列宽,以10字计，得到最佳列宽
		setColumns(sheet,10);
		
		//行计数器
		int rowNum = 0;
		//处理题目
		{
			//在sheet中创建一个新行（题目）
			HSSFRow title = sheet.createRow(rowNum++);		
			//将0行0列至0行7列的单元格合并为一个单元格
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
			//创建题目行中，第一个，也是惟一单元格
			Cell cell = title.createCell(0);	
			//为题目单元格赋值
			cell.setCellValue("毕业设计(论文)题目申报");		
		}
		//处理表头
		{
			//在sheet中创建一个新行（表头）
			HSSFRow header = sheet.createRow(rowNum++);
			//为新行创建、填充多个单元格			
			header.createCell(0).setCellValue("题目名称");
			header.createCell(1).setCellValue("类型");
			header.createCell(2).setCellValue("类别");
			header.createCell(3).setCellValue("教师姓名");
			header.createCell(4).setCellValue("教师职称");
			header.createCell(5).setCellValue("教师学历");
			header.createCell(6).setCellValue("限选专业");
			header.createCell(7).setCellValue("审批状态");
		}
		//处理表体
		for (GraduateProject graduateProject : graduateProjectList) {
			//在sheet中创建另一个新行（表体）
			HSSFRow row = sheet.createRow(rowNum++);
			//为新行创建、填充多个单元格
			int column = 0;
			row.createCell(column++).setCellValue(graduateProject.getTitle());
			
			
			
			row.createCell(column++).setCellValue(
					graduateProject.getProjectCategory().getDescription());
			row.createCell(column++).setCellValue(
					graduateProject.getProjectType().getDescription());
			row.createCell(column++).setCellValue(
					graduateProject.getTeacher().getName());
			row.createCell(column++).setCellValue(
					graduateProject.getTeacher().getTitle().getDescription());
			row.createCell(column++).setCellValue(
					graduateProject.getTeacher().getDegree().getDescription());
			Collection<Major> majors = graduateProject.getLimitedToMajor();
			StringBuilder majors_Str = new StringBuilder();
			for (Major major : majors) {
				majors_Str.append(major.getDescription());
				majors_Str.append("/");
			}
			//删除最后一个字符，即"/"
			majors_Str.deleteCharAt(majors_Str.length() - 1);
			row.createCell(column++).setCellValue(majors_Str.toString());
			row.createCell(column++).setCellValue(graduateProject.getGraduateProjectStatus().getDescription());

		}

	}
	//设置列宽
	//一个10号字大约有500像素宽
	
	/**
	 * @param sheet 工作表
	 * @param fontSize 字号
	 */
	private void setColumns(HSSFSheet sheet, int fontSize) {
		int columnIndex=0;
		//20个字
		System.out.println("CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*20="+CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*20);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*20);
		//2个字
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*2);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*4);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*4);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*4);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*4);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*9);
		sheet.setColumnWidth(columnIndex++, CONSTANTS.EXCEL_FONT_SIZE_POINT * fontSize*4);
	}

}
